<template>
  <div class="materialAdd" v-loading="detailInfoShow">
    <!-- 筛选按钮 -->
    <div class="search-button">
      <el-button
        v-if="pageType !== 'detail'"
        class="btn save"
        @click="handleAddSave(materialFormRef)"
        ><el-icon class="icon" color="#fff"> <Tickets /> </el-icon
        >保存</el-button
      >
      <el-button class="btn direction" @click="handleBack"
        ><el-icon class="icon" color="#fff"> <Back /> </el-icon
        >返回</el-button
      >
    </div>
    <el-collapse v-model="activeNames">
      <!-- 基础信息 -->
      <el-collapse-item name="1" class="transform-down">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title"
            >{{
              pageType !== "detail"
                ? pageType === "add"
                  ? "新增"
                  : "编辑"
                : ""
            }}物料信息</span
          >
        </template>
        <div class="material-info">
          <el-form
            :model="materialForm"
            ref="materialFormRef"
            :rules="queryRules"
            label-width="100"
            v-enterToNext
            :disabled="pageType === 'detail'"
          >
            <el-row>
              <el-col :span="6">
                <el-form-item label="分类：" prop="categoryId">
                  <el-tree-select
                    style="width: 268px"
                    v-model="materialForm.categoryId"
                    :data="classOptions"
                    :props="{
                      value: 'id',
                      label: 'categoryName',
                      children: 'children',
                    }"
                    value-key="id"
                    placeholder="请选择"
                    check-strictly
                    @change="handleCategoryChange"
                  >
                    <template
                      #default="{ data: { categoryName, importantDevice } }"
                    >
                      {{ categoryName }}
                      <el-icon
                        v-if="importantDevice == 1"
                        style="margin-left: 5px; transform: translateY(2px)"
                        title="贵重设备"
                        color="#FF9552"
                        :size="15"
                      >
                        <Box />
                      </el-icon>
                    </template>
                  </el-tree-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="物料编号：" prop="materialCode">
                  <el-input
                    v-model="materialForm.materialCode"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="物料简称：" :prop="materialForm.importantDevice == 1 ? 'materialName' : [{required: false}]" label-width="134">
                  <el-input
                    ref="refInput"
                    v-model="materialForm.materialName"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="采购名称：" prop="purchaseName">
                  <el-input
                    v-model="materialForm.purchaseName"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="销售名称："
                  :prop="materialForm.importantDevice == 1 ? 'saleName' : [{required: false}]"
                >
                  <el-input
                    v-model="materialForm.saleName"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
            <!-- </el-row>
            <el-row> -->
              <el-col :span="6">
                <el-form-item label="出厂型号：" prop="factoryCode">
                  <el-input
                    v-model="materialForm.factoryCode"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="销售型号：" label-width="134">
                  <el-input
                    v-model="materialForm.saleCode"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="技术参数：">
                  <el-input
                    v-model="materialForm.technologyParameter"
                    placeholder="请输入"
                    clearable
                    maxlength="20"
                    class="search-input"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="单位："
                  prop="materialUnit"
                  
                >
                  <el-select
                    v-model="materialForm.materialUnit"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in unitList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            <!-- </el-row>
            <el-row> -->
              <el-col :span="6">
                <el-form-item label="预警数量：" prop="warningNumber">
                  <el-input
                    v-model="materialForm.warningNumber"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                    type="number"
                    @input="
                      materialForm.warningNumber =
                        materialForm.warningNumber.replace(/^(0+)|[^\d]+/g, '')
                    "
                  />
                </el-form-item>
              </el-col>
             
              <!-- <el-col :span="6">
                <el-form-item label="存货类型：" prop="stockType">
                  <el-select
                    v-model="materialForm.stockType"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in stockTypeList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col> -->
              <el-col :span="6">
                <el-form-item
                  label="是否允许负库存："
                  prop="underStock"
                  label-width="134"
                >
                  <el-select
                    v-model="materialForm.underStock"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in flagList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              
            <!-- </el-row>
            <el-row> -->
              <el-col :span="6">
                <el-form-item label="供应商：">
                  <el-select
                    filterable
                    v-model="materialForm.supplierId"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <template v-if="suppliereList.length">
                      <el-option disabled :value="{}">
                        <div class="auto-top">
                          <span class="auto-item">供应商编号</span>
                          <span class="auto-item">供应商简称</span>
                          <span class="auto-item">供应商全称</span>
                          <span class="auto-item">供应商地址</span>
                        </div>
                      </el-option>
                      <el-option
                        v-for="dict in suppliereList"
                        :key="dict.supplierId"
                        :value="dict.supplierId"
                        :label="dict.supplierName"
                      >
                        <div style="display: flex">
                          <div class="auto-item" :title="dict.supplierCode">
                            {{ dict.supplierCode }}
                          </div>
                          <div
                            class="auto-item"
                            :title="dict.supplierAbbreviation"
                          >
                            {{ dict.supplierAbbreviation }}
                          </div>
                          <div class="auto-item" :title="dict.supplierName">
                            {{ dict.supplierName }}
                          </div>
                          <div
                            class="auto-item"
                            :title="
                              dict.provinceName
                                ? `${dict.provinceName}${dict.cityName}${dict.areaName}`
                                : ''
                            "
                          >
                            {{ dict.provinceName }}{{ dict.cityName
                            }}{{ dict.areaName }}
                          </div>
                        </div>
                      </el-option>
                    </template>

                    <template v-else>
                      <el-option
                        v-for="dict in []"
                        :key="dict.supplierId"
                        :label="dict.supplierId"
                        :value="dict.supplierId"
                      />
                    </template>
                  </el-select>
                  <!-- <el-autocomplete
                    popper-class="autoclass"
                    v-model="materialForm.supplierName"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入"
                    @select="handleSuppliereSelect($event, 1)"
                    style="width: 268px"
                    clearable
                  >
                    <template #default="{ item }">
                      <div class="auto-top" style="display: flex">
                        <div class="auto-item">供应商编号</div>
                        <div class="auto-item">供应商简称</div>
                        <div class="auto-item">供应商全称</div>
                        <div class="auto-item">供应商地址</div>
                      </div>
                      <div style="display: flex; width: 500px">
                        <div class="auto-item" :title="item.supplierCode">
                          {{ item.supplierCode }}
                        </div>
                        <div
                          class="auto-item"
                          :title="item.supplierAbbreviation"
                        >
                          {{ item.supplierAbbreviation }}
                        </div>
                        <div class="auto-item" :title="item.supplierName">
                          {{ item.supplierName }}
                        </div>
                        <div
                          class="auto-item"
                          :title="`${item.provinceName}${item.cityName}${item.areaName}`"
                        >
                          {{ item.provinceName }}{{ item.cityName
                          }}{{ item.areaName }}
                        </div>
                      </div>
                    </template>
                  </el-autocomplete> -->
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="生产厂商：">
                  <el-select
                    filterable
                    v-model="materialForm.manufacturerId"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <template v-if="suppliereList.length">
                      <el-option disabled :value="{}">
                        <div class="auto-top">
                          <div class="auto-item">供应商编号</div>
                          <div class="auto-item">供应商简称</div>
                          <div class="auto-item">供应商全称</div>
                          <div class="auto-item">供应商地址</div>
                        </div>
                      </el-option>
                      <el-option
                        v-for="dict in suppliereList"
                        :key="dict.supplierId"
                        :value="dict.supplierId"
                        :label="dict.supplierName"
                      >
                        <div style="display: flex">
                          <div class="auto-item" :title="dict.supplierCode">
                            {{ dict.supplierCode }}
                          </div>
                          <div
                            class="auto-item"
                            :title="dict.supplierAbbreviation"
                          >
                            {{ dict.supplierAbbreviation }}
                          </div>
                          <div class="auto-item" :title="dict.supplierName">
                            {{ dict.supplierName }}
                          </div>
                          <div
                            class="auto-item"
                            :title="
                              dict.provinceName
                                ? `${dict.provinceName}${dict.cityName}${dict.areaName}`
                                : ''
                            "
                          >
                            {{ dict.provinceName }}{{ dict.cityName
                            }}{{ dict.areaName }}
                          </div>
                        </div>
                      </el-option>
                    </template>

                    <template v-else>
                      <el-option
                        v-for="dict in []"
                        :key="dict.supplierId"
                        :label="dict.supplierId"
                        :value="dict.supplierId"
                      />
                    </template>
                  </el-select>
                  <!-- <el-autocomplete
                    popper-class="autoclass"
                    v-model="materialForm.manufactureName"
                    :fetch-suggestions="querySearch"
                    placeholder="请输入"
                    @select="handleSuppliereSelect($event, 2)"
                    style="width: 268px"
                    clearable
                  >
                    <template #default="{ item }">
                      <div class="auto-top" style="display: flex">
                        <div class="auto-item">供应商编号</div>
                        <div class="auto-item">供应商简称</div>
                        <div class="auto-item">供应商全称</div>
                        <div class="auto-item">供应商地址</div>
                      </div>
                      <div style="display: flex; width: 500px">
                        <div class="auto-item" :title="item.supplierCode">
                          {{ item.supplierCode }}
                        </div>
                        <div
                          class="auto-item"
                          :title="item.supplierAbbreviation"
                        >
                          {{ item.supplierAbbreviation }}
                        </div>
                        <div class="auto-item" :title="item.supplierName">
                          {{ item.supplierName }}
                        </div>
                        <div
                          class="auto-item"
                          :title="`${item.provinceName}${item.cityName}${item.areaName}`"
                        >
                          {{ item.provinceName }}{{ item.cityName
                          }}{{ item.areaName }}
                        </div>
                      </div>
                    </template>
                  </el-autocomplete> -->
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="默认税率：">
                  <el-select
                    v-model="materialForm.defaultTaxRate"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in taxRateList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="首次采购日期：" label-width="134">
                  <el-date-picker
                    v-model="materialForm.firstPurchaseTime"
                    format="YYYY/MM/DD"
                    class="search-input"
                    value-format="YYYY-MM-DD"
                    type="date"
                    placeholder="请选择"
                  />
                </el-form-item>
              </el-col>
            <!-- </el-row>
            <el-row> -->
              <el-col :span="6">
                <el-form-item label="采购单价：" prop="price">
                  <el-input
                    type="Number"
                    v-model="materialForm.price"
                    placeholder="请输入"
                    clearable
                    class="search-input"
                  >
                    <template #append>元</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item
                  label="是否测评："
                  prop="needTest"
                >
                  <el-select
                    v-model="materialForm.needTest"
                    placeholder="请选择"
                    clearable
                    class="search-input"
                  >
                    <el-option
                      v-for="dict in flagList"
                      :key="dict.value"
                      :label="dict.label"
                      :value="dict.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row
              v-if="
                pageType === 'edit' &&
                materialForm.price &&
                purchasePrice != materialForm.price
              "
              style="padding-left: 15px"
            >
              <el-col :span="24">
                <el-form-item
                  label="采购单价更改说明:"
                  label-width="85"
                  :prop="
                    pageType === 'edit' &&
                    materialForm.price &&
                    purchasePrice != materialForm.price
                      ? 'reason'
                      : ''
                  "
                >
                  <el-input
                    style="width: 100%; margin-top: 7px"
                    v-model="materialForm.reason"
                    type="textarea"
                    maxlength="2000"
                    placeholder="请输入"
                  ></el-input>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6">
                <el-form-item label="物料图片：">
                  <Upload
                    :fileList="materialForm.materialPictures"
                    :isFull="true"
                    :isCanDelete="pageType == 'detail' ? false : true"
                    :disabled="pageType == 'detail'"
                    :showButton="true"
                    accept=".jpg,.png"
                    @success="(result) => uploadSuccess(result)"
                    @deleteFile="(result) => uploadRemove(result)"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </div>
      </el-collapse-item>
      <!-- 采购单价更改记录 -->
      <el-collapse-item name="2" v-if="pageType === 'detail'">
        <template #title>
          <span class="circle"></span>
          <span class="collapse-title">采购单价更改记录</span>
        </template>
        <div class="material-info">
          <!-- 表格数据 -->
          <el-table
            :data="materialForm.tableData"
            border
            height="100%"
            :header-cell-style="{ 'text-align': 'center' }"
          >
            <el-table-column
              label="更新日期"
              prop="createTime"
              :show-overflow-tooltip="true"
              width="160"
            />
            <el-table-column
              label="采购单价（元）"
              prop="price"
              :show-overflow-tooltip="true"
              width="200"
            >
              <template #default="{ row }">
                {{ row.price ? row.price : "0" }}
              </template>
            </el-table-column>
            <el-table-column
              label="备注说明"
              prop="reason"
              :show-overflow-tooltip="true"
              min-width="160"
            >
              <template #default="{ row }">
                {{ row.reason ? row.reason : "初始数据" }}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </el-collapse-item>
    </el-collapse>
  </div>
</template>

<script setup>
import Upload from "@/components/Upload/index";
import { getDictsApi } from "@/api/dict";
const { proxy } = getCurrentInstance();
import { ElMessage, ElMessageBox } from "element-plus";
import { getMaterialClassListApi } from "@/api/information/materialClass";
import { getSupplierListApi } from "@/api/information/supplier";
import {
  addMaterialApi,
  getMaterialApi,
  editMaterialApi,
  getPurchasePriceApi,
} from "@/api/information/material";
const emit = defineEmits([
  // 物料新增组件显隐
  "materialAddShow",
  // 更新物料新增组件显隐
  "update:materialAddShow",
  // 刷新父组件表格数据
  "refreshList",
]);
const props = defineProps({
  // 操作类型
  pageType: {
    type: String,
    default: "add",
  },
});
// 折叠栏默认展开
const activeNames = ref(["1", "2", "3"]);
// 物料表单引用
const materialFormRef = ref();
// 详情加载效果
const detailInfoShow = ref(false);
// 输入框引用
const refInput = ref();
// 单位列表
const unitList = ref([]);
// 存货类型列表
const stockTypeList = ref([]);
// 供应商列表
const suppliereList = ref([]);
// 默认税率列表
const taxRateList = ref([]);
// 销售单价初始值
const purchasePrice = ref(null);

// 物料表单数据模板
const materialFormTemplate = {
  materialId: null,
  categoryId: null, // 物料分类id
  defaultTaxRate: "", // 默认税率
  factoryCode: "", // 	出厂编号
  firstPurchaseTime: "", //	首次采购时间
  manufacturerId: null, // 生产厂商(也是供应商id)
  manufactureName: "", // 生产厂商名称
  materialCode: "", // 物料编号
  materialName: "", // 物料简称
  materialPictures: [], // 	物料图片
  materialUnit: null, //物料单位
  importantDevice:null,
  purchaseName: "", //采购名称
  remark: "", // 	备注
  saleName: "", // 销售名称
  saleCode: "", // 销售型号
  stockType: null, //	存货类型
  supplierId: null, // 	供应商id
  providerName: "", // 供应商名称
  supplierName: "",
  technologyParameter: "", // 技术参数
  underStock: null, // 是否允许负库存
  needTest: null, // 是否测评
  warningNumber: null, // 	预警数量
  reason: "", // 采购说明
  price: 0, // 采购单价
  tableData: [], // 采购记录
  perfect: 0,
};

//自定义数字限制
const digitalRestrictions = (rule, value, callback) => {
  if (value < 0 || value > 9999) {
    callback(new Error("请输入0-9999之内的数字"));
  } else {
    callback();
  }
};
//金额赋值
const moneyValue = (rule, value, callback) => {
  if (!value) {
    materialForm.value.price = 0;
  }
  if (value < 0 || value > 99999999) {
    callback(new Error("请输入0-99999999之内的数字"));
  } else {
    callback();
  }
};
const data = reactive({
  // 物料表单
  materialForm: JSON.parse(JSON.stringify(materialFormTemplate)),
  // 物料表单校验规则
  queryRules: {
    materialCode: [
      {
        required: true,
        message: "请输入物料编号",
        trigger: ["change", "blur"],
      },
    ],
    materialName: [
      {
        required: true,
        message: "请输入物料简称",
        trigger: ["change", "blur"],
      },
    ],
    purchaseName: [
      { required: true, message: "请输入采购名称", trigger: "blur" },
    ],
    saleName: [{ required: true, message: "请输入销售名称", trigger: "blur" }],
    factoryCode: [
      { required: true, message: "请输入出厂型号", trigger: "blur" },
    ],
    technologyParameter: [
      { required: true, message: "请输入技术参数", trigger: "blur" },
    ],
    materialUnit: [
      { required: true, message: "请选择单位", trigger: "change" },
    ],
    warningNumber: [
      { required: true, message: "请输入预警数量", trigger: "blur" },
      { validator: digitalRestrictions, trigger: "blur" },
    ],
    categoryId: [{ required: true, message: "请选择分类", trigger: "change" }],
    stockType: [
      { required: true, message: "请选择存货类型", trigger: "change" },
    ],
    underStock: [
      { required: true, message: "请选择是否允许负库存", trigger: "change" },
    ],
    needTest: [
      { required: true, message: "请选择是否测评", trigger: "change" },
    ],
    price: [{ validator: moneyValue, trigger: "blur" }],
    reason: [
      { required: true, message: "请输入采购单价更改说明", trigger: "blur" },
    ],
  },

  flagList: [
    {
      label: "是",
      value: 1,
    },
    {
      label: "否",
      value: 0,
    },
  ],
  classOptions: [],
  classOptionOrigin: [],
});
const { materialForm, queryRules, flagList, classOptions,classOptionOrigin } = toRefs(data);

/**
 * @description: 保存
 * @param {Object} materialFormRef 物料表单引用
 */
const handleAddSave = async (materialFormRef) => {
  if (!materialFormRef) return;
  await materialFormRef.validate(async (valid, fields) => {
    if (valid) {
      ElMessageBox.confirm("确定保存以上数据吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        confirmButtonClass: "confirmBtnClass",
        cancelButtonClass: "cancelBtnClass",
      })
        .then(async () => {
          const apiFunc = materialForm.value.materialId
            ? editMaterialApi
            : addMaterialApi;
          const res = await apiFunc(materialForm.value);
          if (res.code === 200) {
            emit("update:materialAddShow", false);
            emit("refreshList", true);
            ElMessage.success("操作成功");
            materialForm.value = JSON.parse(
              JSON.stringify(materialFormTemplate)
            );
          } else {
            ElMessage.error(res.msg);
          }
        })
        .catch(() => {});
    } else {
      console.log("error submit!");
    }
  });
};

/**
 * @description: 文件上传成功处理
 * @param {Object} response 文件信息
 */
function uploadSuccess(response) {
  materialForm.value.materialPictures.push({ ...response, businessType: 4 });
}

/**
 * @description: 删除文件
 * @param {Number} index 当前文件索引
 */
function uploadRemove(index) {
  materialForm.value.materialPictures.splice(index, 1);
}

/**
 * @description: 处理分类选择
 * @param {Object} val 信息
 */
const handleCategoryChange = (val)=>{
 if(val) {
  classOptionOrigin.value.forEach(item => {
    if(item.categoryId == val) {
      materialForm.value.importantDevice = item.importantDevice
      if(!item.importantDevice) {
        materialFormRef.value.clearValidate(['materialName','saleName']);
      }
    }
  })
 }else {
  materialForm.value.importantDevice = null
 }
}

/**
 * @description: 获取分类列表
 */
const getMaterialClassList = async () => {
  const res = await getMaterialClassListApi();
  res.rows.forEach((item) => {
    item.id = item.categoryId;
  });
  classOptionOrigin.value = res.rows
  classOptions.value = proxy.handleTree(res.rows, "id");
};

/**
 * @description: 返回
 */
const handleBack = () => {
  if (props.pageType === "detail") {
    emit("update:materialAddShow", false);
    materialForm.value = JSON.parse(JSON.stringify(materialFormTemplate));
  } else {
    ElMessageBox.confirm("确定返回上一级吗?以上数据未保存将会丢失!", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
      confirmButtonClass: "confirmBtnClass",
      cancelButtonClass: "cancelBtnClass",
    })
      .then(async () => {
        emit("update:materialAddShow", false);
        materialForm.value = JSON.parse(JSON.stringify(materialFormTemplate));
      })
      .catch(() => {});
  }
};

/**
 * @description: 获取字典
 */
async function getDicts() {
  // 单位列表
  await getDictsApi("material_unit").then((res) => {
    if (res.code === 200) {
      unitList.value = res.data;
    }
  });
  // 存货类型列表
  await getDictsApi("stock_type").then((res) => {
    if (res.code === 200) {
      stockTypeList.value = res.data;
    }
  });

  // 税率列表
  await getDictsApi("default_tax_rate").then((res) => {
    if (res.code === 200) {
      taxRateList.value = res.data;
    }
  });

  // 供应商列表
  await getSupplierListApi().then((res) => {
    if (res.code === 200) {
      res.rows.forEach((item) => {
        item.value = item.supplierName;
      });
      suppliereList.value = res.rows;
    }
  });
}

/**
 * @description: 处理详情信息
 * @param {Number} id
 */
const handleDetailInfo = async (id) => {
  try {
    detailInfoShow.value = true;
    const res = await getMaterialApi(id);
    const purchaseRes = await getPurchasePriceApi(id);
    materialForm.value = res.data;
    materialForm.value.supplierName = res.data.distributorName;
    purchasePrice.value = res.data.price;
    materialForm.value.reason = "";
    materialForm.value.tableData = purchaseRes || [];
    detailInfoShow.value = false;
  } catch (error) {
    detailInfoShow.value = false;
  }
};

/**
 * @description: 处理供应商、生产厂家选择
 */
// const handleSuppliereSelect = (value, type) => {
//   switch (type) {
//     case 1:
//       materialForm.value.supplierId = value.supplierId;
//       break;
//     case 2:
//       materialForm.value.manufacturerId = value.supplierId;
//       break;
//     default:
//       break;
//   }
// };

onMounted(() => {
  getDicts();
  getMaterialClassList();
  nextTick(() => {
    refInput.value.focus(); // 聚焦
  });
});

// const querySearch = (queryString, cb) => {
//   const results = queryString
//     ? suppliereList.value.filter(createFilter(queryString))
//     : suppliereList.value;
//   cb(results);
// };
// const createFilter = (queryString) => {
//   return (restaurant) => {
//     return (
//       restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0
//     );
//   };
// };

defineExpose({
  handleDetailInfo,
});
</script>
<style>
.autocomplete-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
</style>
<style lang="scss" scoped>
.materialAdd {
  padding: 20px 20px;
  width: 100%;
  height: 100%;
  .el-collapse,
  .el-collapse-item__wrap {
    border: none;
  }

  .search-input {
    width: 268px;
    height: 34px;
  }
  .material-info {
    padding: 20px 10px 5px 10px;
    background-color: #fafafa;
    margin: 10px 0;
  }
  .contact-info {
    padding-top: 20px;
    .table-operate {
      display: flex;
      justify-content: space-around;
      .table-icon {
        width: 20px;
        height: 20px;
        cursor: pointer;
      }
    }
  }
  :deep(.el-form-item--default) {
    margin-bottom: 22px;
  }
  :deep(.el-form-item--default .el-form-item__error) {
    padding-top: 5px;
  }
}
.auto-top {
  width: 100%;
  position: absolute;
  top: 0px;
  display: flex;
  font-weight: 700;
  background: #ffffff;
}
.auto-item {
  width: 125px;
  //text-align: center;
  white-space: nowrap; /* 禁止换行 */
  overflow: hidden; /* 隐藏超出部分 */
  text-overflow: ellipsis; /* 使用省略号表示超出部分 */
}
.el-autocomplete .el-autocomplete-suggestion {
  max-height: 200px; /* 设置最大高度 */
  overflow-y: auto; /* 超出高度时显示滚动条 */
}
</style>
